<?php
/**
 * 成交房源
 */

namespace app\admin\controller;


use app\common\controller\AdminController;
use think\Exception;
use think\facade\Request;
use app\admin\model\HouseStatistics as HouseStatisticsModel;

class HouseStatistics extends AdminController {

    public $levelConfig = [
        1 => ['getlist', 'getpreview2', 'getpreview1'],
        2 => ['submit', 'upload'],
        4 => [],
        8 => []
    ];

    /**
     * 统计列表
     * @return false|string
     */
    public function getList() {
        try {
            $params = $this->checkParams('page,long');
            if ($params === false) return $this->resErr();
            $list = HouseStatisticsModel::getList($params);
            return $this->resData('success', $list);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 预览
     * @return false|string
     */
    public function getPreview2() {
        try {
            if (Request::isGet()) {
                $params = $this->checkParams('key');
                if ($params === false) return $this->resErr();
                $body = HouseStatisticsModel::encodeRespKey($params);
                $request = HouseStatisticsModel::encodeRequestKey($params);
                $head = $request['title'] . '(' . date('Y/m/d', $request['start_time']) . ' - ' . date('Y/m/d', $request['end_time']) . ')';
                $title = $request['city'] . '司法拍卖房 不限购 可贷款';
                $logo = 'https://oss.fpaiyun.com/admin/h5_logo/2021-01-19/35pr1f548cce.png';
                return $this->resData('success', compact('head', 'title', 'logo', 'body'));
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }


    public function getPreview1() {
        try {
            if (Request::isGet()) {
                $params = $this->checkParams('id');
                if ($params === false) return $this->resErr();
                $key = HouseStatisticsModel::preview($params);
                if (!$key) return $this->resErr();
                else $this->resData('success', ['data' => $key]);
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 发布
     * @return false|string
     */
    public function submit() {
        try {
            $params = $this->checkParams('img,key');
            if ($params === false) return $this->resErr();
            $request = HouseStatisticsModel::encodeRequestKey($params);
            $res = HouseStatisticsModel::post($params, $request['id']);
            if (!$res) return $this->resErr();
            else $this->resData('发布成功', []);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 上传
     * @return false|string
     */
    public function upload() {
        try {
            $params = $this->checkParams('');
            if ($params === false) return $this->resErr();
            $file = request()->file('img');
            $file_header = config('global.file_dir');
            $file_domain = config('global.file_domain');
            $info = $file->validate(['size' => 10242880, 'ext' => 'jpg,png,gif,jpeg'])->move($file_header . 'house_statistics/');
            if ($info) {
                $save_file = $file_domain . 'house_statistics/' . $info->getSaveName();
                return $this->resData('success', ['url' => $save_file]);
            } else {
                // 上传失败获取错误信息
                echo $file->getError();
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

}